package com.zxl.grocery.core.webmagic.scheduled;

import com.zxl.grocery.common.persistence.model.CrawlerConfig;
import com.zxl.grocery.core.webmagic.pipeline.NewsPipeline;
import com.zxl.grocery.core.webmagic.processor.DevelopersProcessor;
import com.zxl.grocery.core.webmagic.processor.JianShuProcessor;
import com.zxl.grocery.core.webmagic.processor.ZhiHuProcessor;
import com.zxl.grocery.modular.crawler.service.ICrawlerConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * info:新闻定时任务
 * Created by shang on 16/8/22.
 */
@Component
@EnableScheduling
public class NewsScheduled {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private NewsPipeline newsPipeline;
    @Autowired
    private ICrawlerConfigService iCrawlerConfigService;

    /**
     * 开发者头条
     */
    ////[秒] [分] [小时] [日] [月] [周] [年]
    @Scheduled(cron = "0 0 0/3 * * ? ")//从0点开始,每3个小时执行一次
    public void developersScheduled() {
        HashMap<String,Object> conditionMap = new HashMap<String,Object>();
        conditionMap.put("code","DevelopersScheduled");
        List<CrawlerConfig> crawlerConfigs = iCrawlerConfigService.selectByMap(conditionMap);

        if (crawlerConfigs.size() > 0 ) {
            for (int i = 0; i < crawlerConfigs.size(); i++) {
                if (crawlerConfigs.get(i).getContent().equals("1")){
                    System.out.println("----开始执行开发者头条定时任务");
                    Spider spider= Spider.create(new DevelopersProcessor());
                    spider.addUrl("http://toutiao.io/prev/" + sdf.format(new Date()));
                    spider.addPipeline(newsPipeline);
                    spider.thread(5);
                    spider.setExitWhenComplete(true);
                    spider.start();
                    spider.stop();
                }
            }

        }
    }

    /**
     * 简书
     */
    @Scheduled(cron = "0 0/10 0 * * ? ")//从0点开始,每2个小时执行一次
    public void jianShuScheduled() {
        HashMap<String,Object> conditionMap = new HashMap<String,Object>();
        conditionMap.put("code","JianShuScheduled");
        List<CrawlerConfig> crawlerConfigs = iCrawlerConfigService.selectByMap(conditionMap);

        if (crawlerConfigs.size() > 0 ) {
            for (int i = 0; i < crawlerConfigs.size(); i++) {
                if (crawlerConfigs.get(i).getContent().equals("1")){
                    System.out.println("----开始执行简书定时任务");
                    Spider spider= Spider.create(new JianShuProcessor());
                    spider.addUrl("http://www.jianshu.com");
                    spider.addPipeline(newsPipeline);
                    spider.thread(5);
                    spider.setExitWhenComplete(true);
                    spider.start();
                    spider.stop();
                }
            }
        }
    }

    /**
     * 知乎发现
     */
    @Scheduled(cron = "0 0 0/6 * * ? ")//从0点开始,每4个小时执行一次
    public void zhiHuScheduled() {
        HashMap<String,Object> conditionMap = new HashMap<String,Object>();
        conditionMap.put("code","ZhiHuScheduled");
        List<CrawlerConfig> crawlerConfigs = iCrawlerConfigService.selectByMap(conditionMap);

        if (crawlerConfigs.size() > 0 ) {
            for (int i = 0; i < crawlerConfigs.size(); i++) {
                if (crawlerConfigs.get(i).getContent().equals("1")){
                    System.out.println("----开始执行知乎发现定时任务");
                    Spider spider= Spider.create(new ZhiHuProcessor());
                    spider.addUrl("https://www.zhihu.com/explore");
                    spider.addPipeline(newsPipeline);
                    spider.thread(5);
                    spider.setExitWhenComplete(true);
                    spider.start();
                    spider.stop();
                }
            }
        }
    }

//    @Autowired
//    private Producer producer;
//
//    @Scheduled(cron = "0/20 * * * * ? ")
//    public void sendJms() {
//        System.out.println("每20秒执行一次--------------------");
//        GroceryEvent event=new GroceryEvent();
//        event.setEvent(GroceryEvent.Event.EVENT_TACK_SUCCESS);
//        event.setText("Jms Message to activemq,my name is zxl");
//        producer.send(event,GroceryEvent.SPRAY_TASK_EVENT_QUEUE,new GroceryMessageConverter());
//
//    }

}
